package com.tcu.boiler.factory.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tcu.boiler.factory.entity.FactoryGroup;
import com.tcu.boiler.factory.service.IFactoryGroupService;
import com.tcu.exception.SmartException;
import com.tcu.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/boiler-group")
public class FactoryGroupController {
    private IFactoryGroupService groupService;

    @PostMapping("add")
    public R add(@RequestBody FactoryGroup factoryGroup){
        if (factoryGroup.getMarkId()!=null) {
            throw new SmartException(20001,"id不为空");
        }
        boolean b = groupService.save(factoryGroup);
        return b ? R.success().data("markId",factoryGroup.getMarkId()) : R.error();
    }
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable String id){
        boolean b = groupService.removeById(id);
        return b ? R.success() : R.error();
    }

    @PostMapping("update")
    public R update(@RequestBody FactoryGroup factoryGroup){
        if (factoryGroup.getMarkId()==null) {
            throw new SmartException(20001,"id为空，无法更新");
        }
        boolean b = groupService.updateById(factoryGroup);
        return b ? R.success() : R.error();
    }
    @GetMapping("getById/{id}")
    public R getById(@PathVariable String id){
        FactoryGroup byId = groupService.getById(id);
        return R.success().data("groupInfo", byId);
    }
    @GetMapping("getGroupByFactoryId/{factoryId}")
    public R getGroupByFactoryId(@PathVariable String factoryId){
        QueryWrapper<FactoryGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("factory_id",factoryId);
        List<FactoryGroup> list = groupService.list(wrapper);
        return R.success().data("list", list);
    }
    @GetMapping("getMarkId/{factoryId}/{groupId}")
    public R getMarkId(@PathVariable String factoryId,@PathVariable String groupId){
        String markId = groupService.getMarkIds(factoryId, groupId);
        return R.success().data("markId", markId);
    }

    @Autowired
    public void setGroupService(IFactoryGroupService groupService) {
        this.groupService = groupService;
    }
}
